Xbasic

Overview: Functions and Expressions

Description

Expressions are used throughout Alpha Anywhere for a variety of purposes. They are used to order and select records, display calculations on layouts, link two tables together, and to specify conditions under which events occur. For example, you can create an expression for a calculated field to show sales totals at the bottom of a Report, or you can create a duplicate key expression so that Alpha Anywhere knows which records in your table are duplicates.

No matter what you use expressions for, the syntax you use for creating expressions is the same throughout the program. This chapter contains a detailed discussion of this syntax, letting you get the most from Alpha Anywhere. You will see expressions in many different situations, but the most common are:

  • Index Definitions
  • Filter Expressions
  • Order Expressions

When you want to derive a value by performing computations on variables, fields, and constants, you do it by forming an expression. An expression is like a formula composed of combinations of:

  • Variables
  • Constants
  • Table Field Names
  • Layout Field Names
  • Xbasic Functions
The maximum length of an expression is approximately 950 characters.

Data Types

The result of this formula can be a numeric, a character string, a date, or a logical value.

All expressions return a value which can be used in an Xbasic script. This value can either be assigned to a variable or passed along as a parameter to an Xbasic function.

Alpha Anywhere is very particular about the data type of an expression and the context in which the expression is used. If the data type to which the expression evaluates does not match the expected or required data type, an error will occur.

For example, the TRACE.WRITELN() method requires a character parameter. To print a numeric value in the Trace window, the following statement is used:

num = 5
trace.writeln (str ( num ) )

The expression str(num), which is passed to the TRACE.WRITELN() method, converts the numeric variable to a string.

The following shortcut would also work. It relies on the fact that Alpha Anywhere implicitly converts variable types when concatenating them with character variables:
trace.writeln("" + num)

What is a Filter Expression?

The Filter Expression determines the records you are going to work with. The expression has the following characteristics:

  • It must evaluate to a logical value. When it evaluates to .T. (TRUE), the record is included. When the expression evaluates to .F. (FALSE), the record is excluded.

  • It typically refers to one or more fields in the table (e.g. DOB >= {1/1/48} .and. isnotblank(LASTNAME) ).

  • If may include functions that manipulate or evaluate field values.

  • An empty expression selects all records previously selected by any filter associated with the underlying form or browse. If that expression is empty, you will get all records in the table.

In many situations an expression is passed as a character argument to a function. In these situations the expression must be quoted. Values and variables within quoted expressions must also be quoted. In order to avoid confusion about the meaning of the quotes, there are three optional (but equivalent) methods for quoting values and variables within these quoted expressions. For example:

report.preview("Customer List", "lastname > 'm'")
report.preview("Customer List", "lastname > \"m\"")
report.preview("Customer List", "lastname > " + quote("m"))

Filter expressions may use ASK Variables to get user input at run-time.

Writing a Filter Expression

A filter expression contains specially formatted character data in the format.

Filter_Sub_Expression1 [ .AND. |.OR. Filter_Sub_Expression2 [ ... .AND. |.OR. Filter_Sub_ExpressionN ]]

A filter sub-expression references at least one field in a table in one of the following formats. The following are examples of typical sub-expressions.

Format and Examples

  • Character

    Character_Field = " Character_Value "
    Character_Field = Character_Variable
    left( Character_Field,1) > " Character_Value "
  • Date

    Date_Field = ctod(" Date_Value ")
    Date_Field < Date_Variable
    month( Date_Field ) = Number
  • Logical

    Logical_Field = .T.
    Logical_Field <> Logical_Variable
    .not. Logical_Field = .F.
  • Number

    Number_Field > Number
    Number_Field = Number_Variable
    ceiling( Number_Field ) <= 12

What is an Order Expression?

The Order Expression determines the order in which to display the records you have selected. The expression has the following characteristics:

  • It must evaluate to a character value less than or equal to 240 characters in length.

  • It typically refers to one or more fields in the table (e.g. left(LASTNAME,8)+left(FIRSTNAME,8) ).

  • If may include functions that manipulate or evaluate field values.

  • An empty expression uses the order associated with the underlying form or browse. If that expression is empty, your will get records in record number order.

Complete lists of Alpha functions and methods can be found in Function List and Method List

Key Terms

Expression

Any combination of fields, constant values, functions, and operators that can evaluate to a single value.

Function

A pre-defined keyword used in an expression, Each function has a particular purpose. For example, DATE()is a function to determine the current date.

Operator

A symbol used to represent a mathematical, relational, logical, or string operation. Example operators: +, -, *.

Expression Builder

A visual tool used to create expressions in Alpha Anywhere.

Numeric Expression

An expression that evaluates to a numeric value. For example, 3+4 is a numeric expression that evaluates to 7.

Character Expression

An expression that evaluates to a character value. Order Expressions are always Character Expressions.

Date Expression

An expression that evaluates to a date value.

Logical Expression

An expression that evaluates to either TRUE or FALSE. For example, 2+4=7 is a logical expression that evaluates to FALSE. Filter expressions are always Logical Expressions.

Linking Key Expression

A character expression used to specify common field(s) between two tables.

Conditional Expression

A logical expression used to determine whether or not an event occurs.

Filter Expression

A logical expression used to select records.

Order Expression

A character expression used to sort records.

Calculated Field

A virtual field defined for a table, set or layout. Calculated fields aren't stored on disk. They're calculated as Needed.

Calculated Field Rule

A Field Rule that inserts and expression's value into a field whenever a record is entered or changed.

See Also